#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        if (n == 1) return 0;
        vector<int> buy(n + 1);
        auto sold = buy;
        auto free = buy;
        buy[0] = -prices[0];
        for (int i = 1; i <= n; i++)
        {
            buy[i] = max(buy[i - 1], free[i - 1] - prices[i - 1]);
            sold[i] = buy[i - 1] + prices[i - 1];
            free[i] = max(free[i - 1], sold[i - 1]);
        }
        return max(sold[n], free[n]);
    }
};